package com.bd.scm.module.item.controller;

import com.bd.scm.domain.dto.item.ItemDto;
import com.bd.scm.domain.models.item.Item;
import com.bd.scm.domain.models.system.BdSysCostcenter;
import com.bd.scm.module.item.service.ItemService;
import com.bd.scm.module.utils.ResultUtil;
import com.bd.utils.vo.Result;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统基础数据
 * Created with IntelliJ IDEA.
 * User: _Cps
 * Date: 2019.12.21 11:11
 */
@RestController
@RequestMapping("api/item")
public class ItemController {

    @Resource
    private ItemService itemService;

    @RequestMapping("getItemByTypeId")
    public Result getItemByTypeId(@RequestBody ItemDto itemDto){
        return ResultUtil.ok(itemService.getItemByTypeId(itemDto));
    }

    /**
     * 新增或修改.
     *
     * @param item
     * @return
     */
    @PostMapping("addOrEdit")
    public Result addOrEdit(@RequestBody Item item) {
        Result result = null;
        try {
            String msg = itemService.addOrEdit(item);
            result = Result.ok("保存成功");
            if (null != msg) {
                result = Result.error(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("服务器内部错误，保存失败");
        }
        return result;
    }

    /**
     * 删除.
     *
     * @param item
     * @return
     */
    @PostMapping("deleteBanciType")
    public Result deleteBanciType(@RequestBody Item item) {
        Result result = null;
        try {
            String msg = itemService.deleteBanciType(item);
            result = Result.ok("删除成功");
            if (null != msg) {
                result = Result.error(msg);
            }
        } catch (Exception e) {
            e.printStackTrace();
            result = Result.error("服务器内部错误，删除失败");
        }
        return result;
    }
}
